//url构建
function url_(u) {
    var url = '';
    if (typeof(u) == "string") {
        url = site_url + u;
    }
    return url;
}


function request_(url, data, success) {
    $.ajax({
        xhrFields: {
            withCredentials: true
        },
        timeout: 5000,
        url: url,
        data: data,
        async: true,
        success: function (d) {
        	if(d.code == 1011){
        		my_tips_(d.msg,function(){
        			go_('login');
        		});
        	}else{
	            success(d);
        	}
        }, error: function (e) {
      	//alert(JSON.stringify(e))
			clear_hash();
            console.log(e);
        }
    })
}

//自动登录
function auto_login(cb) {
    request_(
        url_('index/User/auto_login')
        , {token: LINXIANSHENG_CLIENT_ID}
        , function (d) {
            if (d.code == 0) {
                cb();
            } else {
                my_tips_('个人信息过期，请重新登录', function () {
                    go_href_('#/login');
                });
            }
        }
    );
}
/**
 *
 * @param str
 * @private
 */
function go_href_(str) {
    clear_hash();
    var a_ = document.createElement('a');
    $(a_).attr('href', str).addClass('external');
    $(a_)[0].click();
    a_ = null;
}
/**
 * @type {go_href_}
 * @private
 */
function go_(str) {
    clear_hash();
    str = "#/" + str
    var a_ = document.createElement('a');
    $(a_).attr('href', str).addClass('external');
    $(a_)[0].click();
    a_ = null;
}


function search_() {
    go_("search");
}
function search_list(e) {
    go_(sprintf(
        "%s/%s",
        "goods_list",
        encodeURI(e)
    ));
}

//get user info
function g_u_i() {
    request_(
        url_('api/V1/user_info')
        , {}
        , function (d) {
            if (d.code == 0) {

            } else {
                my_tips_(d.msg);
            }
        }
    );
}

$.extend({
	binding:{
		data:{}, 
		init:function(){
			var onchange=function(e){
				var attr = $(e).attr("binging");
				 
					console.info( e );
				if(attr){  
					me.data[attr] = $(this).val(); 
				}
			}
			$("textarea").change(onchange(this));
			$("input").change(onchange(this));
		}
	}
});